import java.lang.System;
import java.util.Map;
import java.util.HashMap;

/**
 * 数组中只出现⼀次的数字
 * 示例 1:
 * 输入: [1,1,2,3,3,4,4,8,8]
 * 输出: 2
 */
public class SingleNonDuplicate {

    public int[] solution2(int[] nums) {
        int bitmask = 0;
        for (int num: nums) bitmask ^= num;
        int diff = bitmask & (-bitmask);
        int x = 0;

        for (int num: nums) {
            if ((num & diff) != 0) {
                x ^= num;
            }
        }

        return new int[]{x, bitmask^x};
    }

    public int[] solution1(int[] nums) {
        Map<Integer, Integer> hashMap = new HashMap();
        for (int n : nums) {
            hashMap.put(n, hashMap.getOrDefault(n, 0) + 1);
        }
        int[] output = new int[2];
        int indx = 0;
        for (Map.Entry<Integer, Integer> item: hashMap.entrySet()) {
            if (item.getValue() == 1) {
                output[indx++] = item.getKey();
            }
        }
        return output;
    }

    public int solution(int[] nums) {
        int temp = 0;
        for (int i =0;i < nums.length; i++) {
            temp ^= nums[i];
        }
        return temp;
    }

    public static void main(String[] args) {
        int[] nums = {1,1,2,3,3,4,4,8,8};
        System.out.println(new SingleNonDuplicate().solution(nums));
    }
}
